<?php
function curl_requests($url, $data = array(), $headers = array())
{
// 1. 初始化
    $ch = curl_init();
// 2. 设置选项，包括URL
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
// curl_setopt($ch, CURLINFO_HEADER_OUT, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
// curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
	//echo http_build_query($data);
    if ($data) {
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
//        print_r($url)."<br>";
    }

    $output = curl_exec($ch);

    if ($output === FALSE) {
        // print_r( curl_getinfo($ch) );
        echo curl_error($ch);
    } else {
        list ($resHeaders, $body) = explode("\r\n\r\n", $output, 2);
		// cookie
		preg_match_all('/set-cookie:(.+?=.+?;)/', $resHeaders, $cookies_s,PREG_PATTERN_ORDER);
		foreach($cookies_s[1] as $value){
			$cookies .=$value;
			//echo $value;
		}
		$ret[] = $cookies;
        $ret[] = $resHeaders;
        $ret[] = $body;
        return $ret;
    }
    // 4. 释放curl句柄
    curl_close($ch);
    return FALSE;
}
function upload_zip($url,$payload,$cookie,$filepath,$filename){
$file = file_get_contents($filepath);
$multipart_boundary = "WebKitFormBoundaryxqBEt3680fbNgnaQ";
$params = "";
foreach($payload as $name=>$value){
    //echo $name.':'.$value.'<\br>';
    $params .= "--".$multipart_boundary."\r\n"
    . "Content-Disposition: form-data; name=\"".$name."\"\r\n\r\n"
    .$value
    ."\r\n";
}
$params  = $params
    . "--".$multipart_boundary."\r\n"
    . "Content-Disposition: form-data; name=\"file[]\"; filename=\"".$filename."\"\r\n"
    . "Content-Type: application/octet-stream\r\n\r\n"
    . $file . "\r\n"
    . "--".$multipart_boundary."--\r\n";
//echo $params; 
//echo $ret[0];
//$first_newline = strpos($params, "\r\n");
//$multipart_boundary = substr($params, 2, $first_newline - 2);
//echo $multipart_boundary;
$headers = array();
$headers[] = 'Content-Length: ' . strlen($params);
$headers[] = 'Content-Type: multipart/form-data; boundary='.$multipart_boundary;
$headers[] = 'Cookie: '.$cookie;
// 1. 初始化
$ch = curl_init();
// 2. 设置选项，包括URL
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
// curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
// curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
//echo http_build_query($data);

curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);


$output = curl_exec($ch);
echo $output;
}
$url = 'https://natpacket.6te.net/ftp/index.php';
$data = array("ftpserver"=>"natpacket.6te.net","username"=>"natpacket.6te.net","password"=>"1399092738","Login"=>"登入","protocol"=>"FTP","state"=>"browse","state2"=>"main","language"=>"zh");
$ret = curl_requests($url,$data ,array());
//var_dump($ret[0]);
//echo $ret[0];
$url = 'https://natpacket.6te.net/ftp/index.php';
$payload = array(
    "skin"=> "shinra",
        "language"=> "en",
        "protocol"=> "FTP",
        "ftpserver"=> "natpacket.6te.net",
        "ftpserverport"=> "",
        "username"=> "natpacket.6te.net",
        "password_encrypted"=> "32CA32570E2A70B7D776",
        "sshfingerprint"=> "",
        "ftpmode"=> "automatic",
        "passivemode"=> "no",
        "viewmode"=> "list",
        "sort"=> "",
        "sortorder"=> "",
        "consent_necessary"=> "",
        "consent_preferences"=> "",
        "consent_statistics"=> "",
        "consent_personalized_ads"=> "",
        "consent_nonpersonalized_ads"=> "",
        "user_email"=> "invalid_user_email",
        "privacy1"=> "0",
        "state"=> "upload",
        "state2"=> "",
        "screen"=> "2",
		//"directory"=>"/",
		//"file[]"=>file_get_contents('./udc.js'),
);
$filepath = 'http://idea.medeming.com/a/jihuoma1.zip';
$cookie = $ret[0];
$filename = "jihuoma.zip";
upload_zip($url,$payload,$cookie,$filepath,$filename);
?>